<?php

namespace App\Console\Commands;

use App\Models\Admin\Area;
use App\Models\Admin\Goods;
use App\Models\Admin\GoodsStore;
use App\Models\Api\AdminUser;
use App\Models\Api\SalesStrategy;
use App\Models\Api\Store;
use App\Models\Api\StoreOrder;
use App\Models\Supply\StoreWithdrawal;
use App\Tools\AliSms;
use App\Tools\Prarea;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class Test extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'test';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     * @return mixed
     */
    public function handle()
    {
        $cartArr = SalesStrategy::getAllGoodsList(47, [139]);
        // $re = AliSms::sendList('15638865245', 4, 'OD20001001');
        // $re = AliSms::sendList('15638865245', 4, 'OD20001001', ['expressCompany' => 1111, 'espressOrder' => 222]);
        v($cartArr);
        $goodsList = Goods::select(['id', 'mini_quantity'])->get()->toArray();
        if ($goodsList) {
            foreach ($goodsList as $kk => $goods) {
                $id = GoodsStore::where(['goods_id' => $goods['id']])->update(['mini_quantity' => $goods['mini_quantity']]);
            }
        }
        echo "更新商品数量：" . count($goodsList);
        v(111);
        //        StoreOrder::addInfoBySid(2, []);
        //        $orderSN = pdOrderSn();
        //        v($orderSN);
        //        Prarea::getJson();
        //        self::storeAdd();
        //        self::goodsAdd();
        //        self::repertoryAdd();
        //        self::storeGoodsAdd();
        //        self::storeOrderAdd();
        //        self::storeOrderGoodsAdd();
        //        self::userAdd();
    }

    public static function storeMoneyAdd()
    {
    }

    /**
     * 添加商户信息
     */
    public static function storeAdd()
    {
        $list = DB::table('dis_distributor')->get()->toArray();
        foreach ($list as $key => $item) {
            $info = [];
            $item = (array)$item;
            $info['id'] = $item['dis_id'];
            $info['pid'] = $item['pid'];
            $info['store_sn'] = $item['number'];
            $info['store_name'] = $item['name'];
            $info['principal_name'] = $item['realname'];
            $info['principal_mobile'] = $item['mobile'];
            $info['principal_email'] = $item['email'];
            $info['principal_address'] = $item['address'];
            $info['principal_card_sn'] = $item['idcard'];


            $info['principal_card_a'] = $item['idcard_positive_url'];
            $info['principal_card_b'] = $item['idcard_reverse_side_url'];
            $info['company_sn'] = $item['reg_num'];
            $info['company_license'] = $item['business_license_url'];
            $info['company_type'] = $item['conpany_nature'];
            $info['company_name'] = $item['company_name'];
            $info['company_addrss'] = $item['company_address'];
            $info['company_corporation'] = $item['legal_representative'];
            $info['company_registered_capital'] = $item['registered_capital'];
            $info['company_create_time'] = $item['company_createtime'];
            $info['company_operating_period'] = $item['operating_period'];
            $info['company_business_scope'] = $item['business_scope'];
            $info['is_replenishment'] = $item['isgoods'];
            $info['remark'] = $item['remark'];

            $info['status'] = $item['status'];
            $info['agent_start_time'] = strtotime($item['qvp_begintime']);
            $info['agent_end_time'] = strtotime($item['qvp_endtime']);
            $info['apply_time'] = strtotime($item['apply_time']);
            $info['created_at'] = strtotime($item['update_time']);
            $info['updated_at'] = strtotime($item['update_time']);
            $info = array_filter($info);
            DB::table('store')->insert($info);
        }
    }

    /**
     * 商品数据导入
     */
    public static function goodsAdd()
    {
        $list = DB::table('enc_goods')->get()->toArray();
        foreach ($list as $key => $item) {
            $info = [];
            $imgList = [];
            $item = (array)$item;
            $imgArr = json_decode($item['goods_img2'], true);
            if ($imgArr['data']) {
                foreach ($imgArr['data'] as $imm) {
                    $imgList[] = $imm['goodsImgUrl'];
                }
            }
            $info['id'] = $item['goods_id'];
            $info['cate_id'] = $item['cate_id'];
            $info['goods_sn'] = $item['goods_number'];
            $info['goods_name'] = $item['goods_name'];
            $info['goods_price'] = $item['goods_price'];
            $info['goods_type'] = $item['goods_type'];
            $info['goods_img'] = $item['goods_img1'];
            $info['goods_img_list'] = $imgList ? json_encode($imgList) : '';
            $info['goods_detail'] = $item['goods_detail'];
            $info['goods_supplier'] = $item['goods_supplier'];
            $info['goods_pdate'] = $item['goods_pdate'];
            $info['goods_expirationdate'] = $item['goods_expirationdate'];
            $info['goods_specification'] = $item['goods_specification'];
            $info['goods_unit'] = $item['goods_unit'];
            $info['goods_barcode'] = $item['goods_barcode'];
            $info['goods_remark'] = $item['goods_remark'];
            $info['status'] = $item['goods_show'];
            $info['created_at'] = strtotime($item['create_time']);
            $info['create_user'] = $item['create_user'];
            $info['updated_at'] = strtotime($item['update_time']);
            $info['update_user'] = $item['update_user'];
            $info = array_filter($info);
            //            v($info);
            DB::table('goods')->insert($info);
        }
    }

    /**
     * 库存添加
     */
    public static function repertoryAdd()
    {
        $list = DB::table('enc_repertory')->get()->toArray();
        foreach ($list as $key => $item) {
            $info = [];
            $item = (array)$item;
            $info['goods_total_num'] = $item['goods_snumber'];
            $info['goods_sale_num'] = $item['goods_unumber'];
            $info = array_filter($info);
            DB::table('goods')->where(['id' => $item['goods_id']])->update($info);
        }

        $list = DB::table('enc_repertory_log')->get()->toArray();
        foreach ($list as $key => $item) {
            $info = [];
            $item = (array)$item;
            $info['order_sn'] = $item['order_number'];
            $info['store_id'] = $item['dis_id'];
            $info['goods_id'] = $item['goods_id'];
            $info['goods_num'] = $item['number'];
            $info['goods_after_num'] = $item['end_number'];
            $info['type'] = $item['type'] == 1 ? 2 : 1;
            $info['remark'] = $item['remark'];
            $info['created_at'] = strtotime($item['create_time']);
            $info['create_user'] = $item['create_user'];
            $info = array_filter($info);
            DB::table('goods_repertory_log')->insert($info);
        }
    }

    /**
     * 商户商品库存添加
     */
    public static function storeGoodsAdd()
    {
        $list = DB::table('dis_repertory')->get()->toArray();
        foreach ($list as $key => $item) {
            $info = [];
            $item = (array)$item;
            $info['store_id'] = $item['dis_id'];
            $info['goods_id'] = $item['goods_id'];
            $info['goods_total_num'] = $item['goods_snumber'];
            $info['goods_total_num'] = $item['goods_unumber'];
            $info['remark'] = $item['remark'];
            $info['created_at'] = strtotime($item['create_time']);
            $info['create_user'] = $item['create_user'];
            $info['updated_at'] = strtotime($item['update_time']);
            $info['update_user'] = $item['update_user'];
            $info = array_filter($info);
            //            v($info);
            DB::table('e_storgoods')->insert($info);
        }

        $list = DB::table('dis_repertory_log')->get()->toArray();
        foreach ($list as $key => $item) {
            $info = [];
            $item = (array)$item;
            $info['order_sn'] = $item['order_number'];
            //            $info['order_id'] = $item['order_id'];
            $info['store_id'] = $item['dis_id'];
            $info['goods_id'] = $item['goods_id'];
            $info['goods_num'] = $item['number'];
            //            $info['goods_money'] = $item['goods_id'];
            $info['goods_after_num'] = $item['end_number'];
            $info['type'] = $item['type'] == 1 ? 2 : 1;
            $info['remark'] = $item['remark'];
            $info['created_at'] = strtotime($item['create_time']);
            $info['create_user'] = $item['create_user'];
            $info = array_filter($info);
            DB::table('e_storgoods_repertory_log')->insert($info);
        }
    }

    /**
     *发货订单添加
     */
    public static function storeOrderAdd()
    {
        $list = DB::table('enc_order')->get()->toArray();
        foreach ($list as $key => $item) {
            $info = [];
            $item = (array)$item;
            $info['id'] = $item['order_id'];
            $info['order_sn'] = $item['order_number'];
            $info['store_id'] = $item['dis_id'];
            $info['order_mobile'] = $item['dis_phone'];
            $info['order_name'] = $item['order_name'];
            $info['order_province'] = $item['order_province'];
            $info['order_city'] = $item['order_city'];
            $info['order_area'] = $item['order_area'];
            $info['order_address'] = $item['order_address'];
            $info['order_amount'] = $item['order_amount'];
            $info['goods_amount'] = $item['goods_amount'];
            $info['order_status'] = $item['order_status'];
            $info['total_number'] = $item['total_number'];
            $info['order_receiver'] = $item['order_receiver'];
            $info['send_status'] = $item['send_status'];
            $info['order_time'] = strtotime($item['order_time']);
            $info['tracking_number'] = $item['tracking_number'];
            $info['email'] = $item['email'];
            $info['freight'] = $item['freight'];
            $info['express'] = $item['express'];
            $info['express_company'] = $item['express_company'];
            $info['remark'] = $item['remark'];
            $info['created_at'] = strtotime($item['create_time']);
            $info['create_user'] = $item['create_user'];
            $info['updated_at'] = strtotime($item['update_time']);
            $info['update_user'] = $item['update_user'];
            $info = array_filter($info);
            //            v($info);
            DB::table('e_store_order')->insert($info);
        }
    }

    /**
     *发货订单商品添加
     */
    public static function storeOrderGoodsAdd()
    {
        $list = DB::table('enc_item')->get()->toArray();
        foreach ($list as $key => $item) {
            $info = [];
            $item = (array)$item;
            $info['order_id'] = $item['order_id'];
            $info['goods_id'] = $item['goods_id'];
            $info['store_id'] = $item['dis_id'];
            $info['goods_number'] = $item['goods_number'];
            $info['goods_price'] = $item['goods_price'];
            $info['goods_amount'] = $item['goods_amount'];
            $info['remark'] = $item['remark'];
            $info['created_at'] = strtotime($item['create_time']);
            $info['create_user'] = $item['create_user'];
            $info['updated_at'] = strtotime($item['update_time']);
            $info['update_user'] = $item['update_user'];
            $info = array_filter($info);
            //            v($info);
            DB::table('e_store_order_goods')->insert($info);
        }
    }

    /**
     * 导入用户
     */
    public static function userAdd()
    {
        // php artisan test
        $list = DB::table('sys_user')->get()->toArray();
        $safe = 'abcd';
        $password = md5('123456' . $safe);
        foreach ($list as $key => $val) {
            $val = (array)$val;
            if ($val['user_type'] == 1) {
                $storeId = DB::table('dis_distributor')->where(['mobile' => $val['phone']])->value('dis_id');
                if (!$storeId) {
                    continue;
                }
            } else {
                $storeId = 0;
            }
            $user = array();
            $user['store_id'] = $storeId;
            $user['role_id'] = $val['user_type'] == 0 ? 1 : 2;
            $user['mobile'] = $val['phone'];
            $user['username'] = $val['username'];
            $user['safe'] = $safe;
            $user['password'] = $password;
            $user['created_at'] = strtotime($val['create_time']);
            $user['updated_at'] = strtotime($val['update_time']);
            $status = 1;
            if ($val['lock_flag'] != 0 || $val['del_flag'] != 0) {
                $status = 2;
            }
            $user['status'] = $status;
            DB::table('e_admin_user')->insert($user);
        }
    }
}